package com.yunyi.camunda.service.impl;

import com.yunyi.camunda.pojo.Result;
import com.yunyi.camunda.pojo.dto.User;
import com.yunyi.camunda.pojo.vo.DeployRequestParam;
import com.yunyi.camunda.service.ProcessDefinitionService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 部署流程定义service实现
 *
 * @author yunyi
 */
@Service
public class ProcessDefinitionServiceImpl implements ProcessDefinitionService {

    @Autowired
    private RepositoryService repositoryService;

    @Override
    public Result<String> deploy(User user, DeployRequestParam requestParam) {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource(requestParam.getResourcePath())
                .name(requestParam.getBpmnName())
                //不需要租户的概念可以把这行去掉
                //.tenantId("tTenantId")
                .deploy();
        if (deploy == null) {
            return Result.fail();
        }
        return Result.success(deploy.getId());
    }

    @Override
    public Result<String> deleteDeployment(String deploymentId) {
        repositoryService.deleteDeployment(deploymentId);
        return Result.successMessage("删除成功");
    }
}
